package com.xiexiaozhou.helper.web.controller;

import com.xiexiaozhou.helper.core.domain.Customer;
import com.xiexiaozhou.helper.core.domain.Order;
import com.xiexiaozhou.helper.core.service.OrderService;
import com.xiexiaozhou.helper.web.from.OrderForDeliverForm;
import com.xiexiaozhou.helper.web.from.OrderForSellForm;
import com.xiexiaozhou.helper.web.from.OrderForTakeForm;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 订单
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 进入订单首页
     *
     * @return
     */
    @GetMapping("/index")
    public String index() {
        return "order/index";
    }

    /**
     * 进入帮我买页面
     *
     * @return
     */
    @GetMapping("/addForSell")
    public String addForSell() {
        return "order/addForSell";
    }

    /**
     * 执行帮我买下单操作，跳转到下单成功页面
     *
     * @param orderForSellForm
     * @param bindingResult
     * @param session
     * @param model
     * @return
     */
    @PostMapping("/addForSell")
    public String addForSell(@Validated OrderForSellForm orderForSellForm,
                             BindingResult bindingResult,
                             HttpSession session,
                             Model model) {
        //清除session中已存在的订单信息
        if (session.getAttribute("orderTemporary") != null) {
            session.removeAttribute("orderTemporary");
        }

        //将订单信息保存到session
        session.setAttribute("orderTemporary", orderForSellForm);

        //表单字段验证
        if (bindingResult.hasErrors()) {
            String errorMsg = "";
            List<ObjectError> objectErrorList = bindingResult.getAllErrors();
            for (ObjectError error : objectErrorList) {
                errorMsg = errorMsg + error.getDefaultMessage() + " ";
            }
            model.addAttribute("errorMsg", errorMsg);
            return "/order/addForSell";
        }

        //从session获取下单客户信息
        Customer customer = (Customer) session.getAttribute("customer");

        //对象赋值
        Order order = new Order();
        BeanUtils.copyProperties(orderForSellForm, order);

        //调用下单方法
        Order result = orderService.create(order, customer.getId());

        //下单成功,跳转到下单成功页面
        if (result != null) {
            return "/order/success";
        }

        return null;
    }

    /**
     * 进入帮我取下单页面
     *
     * @return
     */
    @GetMapping("/addForTake")
    public String addForTake() {
        return "/order/addForTake";
    }

    /**
     * 执行帮我取下单操作，跳转到下单成功页面
     *
     * @param orderForTakeForm
     * @param bindingResult
     * @param session
     * @param model
     * @return
     */
    @PostMapping("/addForTake")
    public String addForTake(
            @Validated OrderForTakeForm orderForTakeForm,
            BindingResult bindingResult,
            HttpSession session,
            Model model) {
        //清除session中已存在的订单信息
        if (session.getAttribute("orderTemporary") != null) {
            session.removeAttribute("orderTemporary");
        }

        //将订单信息保存到session
        session.setAttribute("orderTemporary", orderForTakeForm);

        //表单字段验证
        if (bindingResult.hasErrors()) {
            String errorMsg = "";
            List<ObjectError> objectErrorList = bindingResult.getAllErrors();
            for (ObjectError error : objectErrorList) {
                errorMsg = errorMsg + error.getDefaultMessage() + " ";
            }
            model.addAttribute("errorMsg", errorMsg);
            return "order/addForTake";
        }

        //从session获取下单客户信息
        Customer customer = (Customer) session.getAttribute("customer");

        //对象赋值
        Order order = new Order();
        BeanUtils.copyProperties(orderForTakeForm, order);

        //调用下单方法
        Order result = orderService.create(order, customer.getId());

        //下单成功,跳转到下单成功页面
        if (result != null) {
            return "/order/success";
        }

        return null;
    }

    /**
     * 进入帮我送下单页面
     *
     * @return
     */
    @GetMapping("/addForDeliver")
    public String addForDeliver() {
        return "/order/addForDeliver";
    }

    /**
     * 执行帮我送下单操作，跳转到下单成功页面
     *
     * @param orderForDeliverForm
     * @param bindingResult
     * @param model
     * @param session
     * @return
     */
    @PostMapping("/addForDeliver")
    public String addForDeliver(
            @Validated OrderForDeliverForm orderForDeliverForm,
            BindingResult bindingResult,
            Model model,
            HttpSession session) {
        //清除session中已存在的订单信息
        if (session.getAttribute("orderTemporary") != null) {
            session.removeAttribute("orderTemporary");
        }

        //将订单信息保存到session
        session.setAttribute("orderTemporary", orderForDeliverForm);

        //表单字段验证
        if (bindingResult.hasErrors()) {
            String errorMsg = "";
            for (ObjectError error : bindingResult.getAllErrors()) {
                errorMsg = errorMsg + error.getDefaultMessage() + " ";
            }
            model.addAttribute("errorMsg", errorMsg);
            return "/order/addForDeliver";
        }

        //从session获取下单客户信息
        Customer customer = (Customer) session.getAttribute("customer");

        //对象赋值
        Order order = new Order();
        BeanUtils.copyProperties(orderForDeliverForm, order);

        //调用下单方法
        Order result = orderService.create(order, customer.getId());

        //下单成功,跳转到下单成功页面
        if (result != null) {
            return "/order/success";
        }

        return null;
    }

    /**
     * 订单列表
     *
     * @param start
     * @param limit
     * @param session
     * @param model
     * @return
     */
    @GetMapping("/lists")
    public String lists(
            @RequestParam(defaultValue = "0", value = "start") Integer start,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit,
            HttpSession session,
            Model model) {
        Customer customer = (Customer) session.getAttribute("customer");

        Sort sort = new Sort(Sort.DEFAULT_DIRECTION, "id");
        Pageable pageable = new PageRequest(start, limit, sort);
        Page<Order> orderPage = orderService.listForSelf(customer, pageable);
        model.addAttribute("orderPage", orderPage);

        return "/order/lists";
    }

    @GetMapping("/detail")
    public String detail(
            @RequestParam Long orderId,
            Model model) {
        Order order = orderService.get(orderId);
        model.addAttribute("order", order);
        return "/order/detail";
    }

    /**
     * 支付订单
     *
     * @param orderId
     * @return
     */
    @GetMapping("/pay")
    public String pay(@RequestParam Long orderId) {
        Order order = orderService.pay(orderId);
        return "redirect:/order/lists";
    }

    @GetMapping("/listsForDistribution")
    public String listsForDistribution(
            HttpSession session,
            Model model) {
        Customer customer = (Customer) session.getAttribute("customer");
        List<Order> orderList = orderService.assign(customer);
        model.addAttribute("orderList", orderList);
        return "order/listsForDistribution";
    }

    @GetMapping("/claim")
    public String claim(
            @RequestParam Long orderId,
            HttpSession session
    ) {
        Order order = orderService.claim(orderId);
        return "redirect:/order/listsForDistribution";
    }

    @GetMapping("/confirmByDistributor")
    public String confirmByDistributor(
            @RequestParam Long orderId,
            HttpSession session
    ) {
        Order order = orderService.confirmByDistributor(orderId);
        return "redirect:/order/listsForDistribution";
    }

    @GetMapping("/confirmByCustomer")
    public String confirmByCustomer(@RequestParam Long orderId, Model model) {
        Order order1 = orderService.get(orderId);
        if (order1.getStatus() == 5) {
            Order order = orderService.confirmByCustomer(orderId);
        }

        return "redirect:/order/lists";
    }

    @GetMapping("/evaluate")
    public String evaluate(Model model,@RequestParam Long id) {
        Order order = orderService.get(id);
        model.addAttribute("order",order);
        return "/order/evaluate";
    }
    /**
     * 评价订单
     *
     * @param id
     * @param evaluate
     * @return
     */
    @PostMapping("/evaluate")
    public String evaluate(
            @RequestParam Long id,
            @RequestParam String evaluate
    ) {
        Order order = orderService.evaluate(id, evaluate);
        if (order != null) {
            return "redirect:/order/lists";
        }
        return null;
    }

}
